package com.geist.soulmates.dao;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.geist.soulmates.domain.User;

public class UserDaoImpl extends JdbcDaoSupport implements UserDao {

	@Override
	public boolean createUser(User user) throws DataAccessLayerException {
		try {
			String insertQuery = "INSERT INTO user_details(first_name,gender,user_name,password) VALUES(?,?,?,?)";
			int noOfRows = getJdbcTemplate().update(insertQuery, user.getFirstName(), user.getGender(),
					user.getUserName(), user.getPassword());
			return noOfRows > 0;
		} catch (Exception exception) {
			throw new DataAccessLayerException(exception);
		}
	}

	@Override
	public User getUser(String userName, String password) throws DataAccessLayerException {
		try {
			String selectQuery = "SELECT * FROM user_details WHERE user_name = ? AND password = ?";
			RowMapper<User> rowMapper = new BeanPropertyRowMapper<User>(User.class);
			return getJdbcTemplate().queryForObject(selectQuery, rowMapper, userName, password);
		} catch (Exception exception) {
			throw new DataAccessLayerException(exception);
		}
	}

	@Override
	public boolean deleteUser(User user) throws DataAccessLayerException {
		try {
			String deleteQuery = "DELETE FROM user_details WHERE id = ?";
			return getJdbcTemplate().update(deleteQuery, user.getId()) > 0;
		} catch (Exception exception) {
			throw new DataAccessLayerException(exception);
		}
	}
}
